﻿<UserControl x:Class="LINQPad.OpenAccess.UI.View.BackendSettingsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:conv="clr-namespace:LINQPad.OpenAccess.UI.Converters"
             xmlns:local="clr-namespace:LINQPad.OpenAccess.UI.View"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <!-- control templates -->
        <ControlTemplate x:Key="BackendSelector" TargetType="Control">
            <DockPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto"  />
                    </Grid.RowDefinitions>
                    <!-- use default-->
                    <CheckBox x:Name="UseDefaultBackend" Grid.Row="0" Grid.ColumnSpan="2"
                              IsChecked="{Binding UseDefaultBackend, Mode=TwoWay}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="use default" />
                            <TextBlock x:Name="DefaultBackendPresenter" Foreground="Green"
                                       Text="{Binding DefaultBackend, StringFormat='{} ({0})', Mode=OneWay}"
                                       Visibility="Collapsed" />
                        </StackPanel>
                    </CheckBox>
                    <!-- custom selection -->
                    <TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" 
                           Text="custom: " />
                    <ComboBox x:Name="BackendSelector" Grid.Row="1" Grid.Column="2"
                          MinWidth="150" VerticalAlignment="Top" HorizontalAlignment="Stretch"
                          ItemsSource="{Binding SupportedBackends}" 
                          SelectedItem="{Binding SelectedBackend, Mode=TwoWay}" />
                    <TextBlock x:Name="BackendSelectorErrorHint" Style="{DynamicResource ErrorHintTextBlockStyle}"
                               Grid.Row="1" Grid.Column="2" Margin="5 2" IsHitTestVisible="False"
                               VerticalAlignment="Center" Visibility="Collapsed" />
                </Grid>
            </DockPanel>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding CanUseDefaultBackend, FallbackValue=False}" Value="False">
                    <Setter TargetName="UseDefaultBackend" Property="IsEnabled" Value="False" />
                </DataTrigger>
                <DataTrigger Binding="{Binding CanUseDefaultBackend, FallbackValue=False}" Value="True">
                    <Setter TargetName="DefaultBackendPresenter" Property="Visibility" Value="Visible" />
                </DataTrigger>
               <DataTrigger Binding="{Binding SelectedBackend}" Value="{x:Null}" >
                    <Setter TargetName="BackendSelectorErrorHint" Property="Text" Value="please select..." />
                    <Setter TargetName="BackendSelectorErrorHint" Property="Foreground" Value="Black" />
                    <Setter TargetName="BackendSelectorErrorHint" Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding HasSupportedBackends, FallbackValue=False}" Value="False" >
                    <Setter TargetName="BackendSelectorErrorHint" Property="Text" Value="no backends available !" />
                    <Setter TargetName="BackendSelectorErrorHint" Property="Foreground" Value="Red" />
                    <Setter TargetName="BackendSelectorErrorHint" Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding UseDefaultBackend, FallbackValue=False}" Value="True">
                    <Setter TargetName="BackendSelector" Property="IsEnabled" Value="False" />
                    <Setter TargetName="BackendSelectorErrorHint" Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <ControlTemplate x:Key="ProviderSelector" TargetType="Control">
            <Grid>
                <ComboBox x:Name="ProviderSelector" HorizontalAlignment="Stretch" MinWidth="150" VerticalAlignment="Top"
                                                  ItemsSource="{Binding ProviderNames}" 
                                                  SelectedItem="{Binding SelectedProviderName, Mode=TwoWay}" />
                <TextBlock x:Name="ProviderSelectorErrorHint" Style="{DynamicResource ErrorHintTextBlockStyle}"
                           Margin="5 2" IsHitTestVisible="False"
                           VerticalAlignment="Center" Visibility="Collapsed" />
                <TextBox x:Name="DefaultProvider" HorizontalAlignment="Stretch" Style="{DynamicResource ReadOnlyTextBoxStyle}"
                         Text="{Binding DefaultProviderName, Mode=OneWay, ValidatesOnDataErrors=True}" 
                         Visibility="Collapsed"/>
            </Grid>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding SelectedProviderName}" Value="{x:Null}">
                    <Setter TargetName="ProviderSelectorErrorHint" Property="Visibility" Value="Visible" />
                    <Setter TargetName="ProviderSelectorErrorHint" Property="Foreground" Value="Black" />
                    <Setter TargetName="ProviderSelectorErrorHint" Property="Text" Value="please select..." />
                </DataTrigger>
                <DataTrigger Binding="{Binding HasProviderNames, FallbackValue=False}" Value="False">
                    <Setter TargetName="ProviderSelectorErrorHint" Property="Visibility" Value="Visible" />
                    <Setter TargetName="ProviderSelectorErrorHint" Property="Foreground" Value="Red" />
                    <Setter TargetName="ProviderSelectorErrorHint" Property="Text" Value="no providers available !" />
                </DataTrigger>
                <DataTrigger Binding="{Binding UseDefaultBackend, FallbackValue=False}" Value="True">
                    <Setter TargetName="ProviderSelectorErrorHint" Property="Visibility" Value="Collapsed" />
                    <Setter TargetName="ProviderSelector" Property="Visibility" Value="Collapsed" />
                    <Setter TargetName="DefaultProvider" Property="Visibility" Value="Visible" />
                </DataTrigger>

            </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Resources>
    <DockPanel>
        <GroupBox DockPanel.Dock="Top" Header="backend" Padding="5 2">
            <Control Template="{StaticResource BackendSelector}" />
        </GroupBox>
        <GroupBox DockPanel.Dock="Top" Header="provider" Padding="5 2">
            <Control Template="{StaticResource ProviderSelector}" />
        </GroupBox>
        <GroupBox DockPanel.Dock="Top" Header="settings" Padding="5 2">
            <TextBlock Text="logging/tracing settings not yet implemented :(" />
        </GroupBox>
    </DockPanel>
</UserControl>
